<?
namespace Api\View
{
	/** 
	 * Allow to view content using SMARTY templates or return generated
     * html code to featch variable
	 */
	class Content extends \Api\View{
	   
        /**
         * Display SMART-y template
         * @return value to smarty variable ContentInclude
         */
       
		final public function display($template=null, $template_dir = null){
		 //   $tpl = ($template_dir!=null?$template_dir.'/'.$template:$this->m_params[\Api\ObjectLoader::C_CONTROLLER].'/'.$this->m_params[\Api\ObjectLoader::C_ACTION]).'.tpl';
                        			
            $tpl = $this->resolveTemplate($template, $template_dir);                                    
            $this->m_smarty->assign('ContentInclude',$tpl);            
		}   
        
        
        /**
         * Fetch and return 
         * @return string 
         */
         public function fetchTmp($template=null, $template_dir = null)
         {
            $tpl = $this->resolveTemplate($template, $template_dir);                                    
            return  $this->m_smarty->fetch($tpl);   
         }
        
        
        
        /**
         * Return currently used template path
         * @return string
         */
        private function resolveTemplate($template=null, $template_dir = null)        
        {
            $tpl = ($template_dir!=null?$template_dir:$this->m_params[\Api\ObjectLoader::C_CONTROLLER]).'/'
            .($template==null? $this->m_params[\Api\ObjectLoader::C_ACTION]:$template).'.tpl';
            
			$tpl_file = $this->m_smarty->template_dir[0].$tpl;
			$msg = addslashes($this->m_params[\Api\ObjectLoader::C_CONTROLLER]);
            
			if(!file_exists($tpl_file)) 
				throw new \Exception("CONTENT: Cannot find template [".$tpl_file."]".$msg);
                
            return $tpl;
        }  
        
        
        /**
         * Clone 
         */
        public function __clone()
        {                        
            return new \Api\View\Content(array('Clone'=>true), $this->m_params);
        }    
	}
	
}
?>